package com.woohua.thread.completefeature;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/***
 * @title SimpleTask
 * @description： 简单异步任务执行
 * @author woo hua
 * @version 1.0.0
 * @create 2023/3/21 9:05
 **/
public class SimpleTask {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 1、 runAsync 执行一个异步任务，没有返回值
        CompletableFuture.runAsync(() -> System.out.println("1. runAsync"));
        sleep(100);

        // 2、supplyAsync 执行一个异步任务，有返回值
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("2.1 supplyAsync task be called");
            sleep(100);
            return "2.2 supplyAsync return value";
        });
        System.out.println("2.3 after supplyAsync");
        System.out.println(future.get());
        sleep(200);
    }

    private static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
